package hw02;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;


/**
 * The entry point to the application.
 * 
 * @author Joshua Belanger
 * @version 1.0
 */
public class FunctionPage extends WebPage {
		
	public FunctionPage(HttpServletRequest request) {
		super(request);		
	}

	@Override
	protected void initDocument() {
		super.initDocument();

		String functionName = request.getParameter("function");
		

		Element title = document.createElement("title");
		title.setTextContent("Function Page - " + functionName);
		head.appendChild(title);
		
		Element function = document.createElement("div");
		body.appendChild(function);
		
		Element h2 = document.createElement("h2");
		h2.setTextContent("Input Fields for Function Arguments");
		function.appendChild(h2);
		
		Element form = document.createElement("form");
		form.setAttribute("method", "post");
		form.setAttribute("action", "?");
		function.appendChild(form);

		List<String> parameterNames = Meta.methodInput.get(functionName);
		Iterator<String> iter = parameterNames.iterator();
		if(iter.hasNext()) {
			Element table = document.createElement("table");
			form.appendChild(table);
			
			while(iter.hasNext()) {
				String parameterName = iter.next();
				Element tr = document.createElement("tr");
				table.appendChild(tr);
				Element label = document.createElement("label");
				label.setAttribute("for", parameterName);
				label.setTextContent(parameterName + ":");
				Element td = document.createElement("td");
				td.appendChild(label);
				tr.appendChild(td);
				Element input = document.createElement("input");
				input.setAttribute("id", parameterName);
				input.setAttribute("name", parameterName);
				td = document.createElement("td");
				td.appendChild(input);
				tr.appendChild(td);
			}
		} else {
			Element emptyP = document.createElement("p");
			form.appendChild(emptyP);
			Element emptyEm = document.createElement("em");
			emptyEm.setTextContent("None");
			emptyP.appendChild(emptyEm);
		}
		
		Element div = document.createElement("div");
		form.appendChild(div);

		Element qInput = document.createElement("input");
		qInput.setAttribute("type", "hidden");
		qInput.setAttribute("name", "q");
		qInput.setAttribute("value", ResultPage.class.getName());
		div.appendChild(qInput);
		
		Element functionInput = document.createElement("input");
		functionInput.setAttribute("type", "hidden");
		functionInput.setAttribute("name", "function");
		functionInput.setAttribute("value", functionName);
		div.appendChild(functionInput);
		
		Element proceed = document.createElement("input");
		proceed.setAttribute("type", "submit");
		proceed.setAttribute("value", "Proceed");
		div.appendChild(proceed);
	}
	
	@Override
	protected Element createIdentInfo() {
		Element identInfoDiv = super.createIdentInfo();
		String functionName = request.getParameter("function");
		Element functionElement = document.createElement("span");
		functionElement.setTextContent(functionName);
		identInfoDiv.appendChild(label("Function:", functionElement));
		return identInfoDiv;
	}
	
}
